﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DataCoreTechnologies.Scribe.Business.Html
{
    internal class DefaultValuesDocument : Document
    {
        #region Internal
        //Constructor
        internal DefaultValuesDocument(SqlServer.Database database)
        {
            DocumentType = DocumentType.DefaultValues;
            RootDatabase = database;
            RootServer = RootDatabase.Server;
            URL = "index." + database.Name + "." + "defaultvalues.html";
            _database = database;
        }
        //Properties
        internal override string DisplayName { get { return "Default Values"; } }
        //Methods
        internal override string GetHtml()
        {
            if (_html == "")
            {
                AddCssLine(@"      	#mainBody *.nameColumn {width: 300px; word-wrap: break-word;}");
                AddCssLine(@"      	#mainBody *.tableColumn {width: 250px; word-wrap: break-word;}");
                AddCssLine(@"      	#mainBody *.columnColumn {width: 20%; word-wrap: break-word;}");
                AddCssLine(@"      	#mainBody *.formulaColumn {word-wrap: break-word;}");

                AddBodyLine(@"<div id=""mainBody"">");

                string heirarchy = @"<h2 id=""topNav"">Location: <a href=""http://index.html"">" + RootServer.Name + @"</a> . ";
                heirarchy += @"<a href=""http://index." +
                    _database.Name + @".html"">" + ScrubHtml(_database.Name) + @"</a></h2>";
                AddBodyLine(heirarchy);
                AddBodyLine(@"<h2 id=""title"">" + ScrubHtml(_database.Name) + @" Default Values</h2>");

                AddBodyLine(@"<table>");
                AddBodyLine(@"<colgroup><col class=""nameColumn""><col class=""tableColumn"">" +
                    @"<col class=""columnColumn""><col class=""formulaColumn"">" +
                    @"<col class=""createDateColumn""></colgroup>");
                AddBodyLine(@"<tr><th>Name</th><th>Table</th><th>Column</th><th>Formula</th><th>Creation Date</th></tr>");

                var defaultValues = (from schema in _database.Schemas
                                    from table in schema.Tables
                                    from column in table.Columns
                                    where column.DefaultValue != null
                                        && column.DefaultValue.Name != null
                                    select column);

                Boolean evenRow = true;
                foreach (SqlServer.Column defaultValue in defaultValues)
                {
                    StringBuilder sb = new StringBuilder();
                    
                    if (evenRow == true) 
                        sb.Append(@"<tr class=""evenRow"">");
                    else
                        sb.Append(@"<tr class=""oddRow"">");
                    evenRow = !evenRow;

                    sb.Append(@"<td>" + ScrubHtml(defaultValue.DefaultValue.Name) + @"</td>");

                    string href = @"<td>";
                    if (Business.FilterPreferences.ShowTablesPage)
                    {
                       href += @"<a href=""http://index." + RootDatabase.Name
                           + "." + defaultValue.Table.Schema.Name + ".table."
                           + defaultValue.Table.Name + @".html"">"
                           + ScrubHtml(defaultValue.Table.Schema.Name) + "."
                           + ScrubHtml(defaultValue.Table.Name) + @"</a>";
                    }
                    else
                    {
                       href += ScrubHtml(defaultValue.Table.Schema.Name) + "."
                           + ScrubHtml(defaultValue.Table.Name);
                    }
                    href += @"</td>";
                    sb.Append(href);

                    href = @"<td>";
                    href += @"<a href=""http://index." + RootDatabase.Name
                        + "." + defaultValue.Table.Schema.Name + ".table."
                        + defaultValue.Table.Name + @".html"">"
                        + ScrubHtml(defaultValue.Name) + @"</a>";
                    href += @"</td>";
                    sb.Append(href);

                    sb.Append(@"<td>" + ScrubHtml(defaultValue.DefaultValue.Formula) + @"</td>");
                    sb.Append(@"<td>" + defaultValue.DefaultValue.CreateDate.ToString() + @"</td></tr>");

                    AddBodyLine(sb.ToString());
                }

                AddBodyLine(@"</table>");
                AddBodyLine(@"</div>");
                _html = CreateHtmlDocument();
            }
            return _html;
        }
        #endregion
        #region Private
        private SqlServer.Database _database;
        #endregion
    }
}
